package WinterHomework.day11;

//Leetcode35号问题，搜索插入位置
public class Demo01 {
    public int searchInsert(int[] nums, int target) {
        int left = 0, right = nums.length - 1;
        //答案
//        if (target < nums[left]) return 0;
//        if (target > nums[right]) return nums.length;
//        while (left <= right) {
//            int mid = (left + right) / 2;
//            if (nums[mid] < target) {
//                left = mid + 1;
//            }else if (nums[mid] > target) {
//                right = mid - 1;
//            }else {
//                return mid;
//            }
//        }
//        return left;
        //力扣答案
        int ans = nums.length;
        while (left <= right) {
            int mid = (left + right) / 2;
            if (target <= nums[mid]) {
                ans = mid;
                right = mid - 1;
            }else {
                left = mid + 1;
            }
        }
        return ans;
    }
}
